package oboe.controllers;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import oboe.core.ISigSlot;
import oboe.core.SigSlot;

/**
 * ...
 * @author Felipe Manga
 */

class Controller extends SigSlot
{
	public var asset(default, setAsset):DisplayObject;
	
	public function new( ?asset:DisplayObject ) 
	{
		super();
		this.asset = asset;
		if ( this.asset == null )
		{
			this.asset = new Sprite();
		}
	}
	
	function setAsset( d:DisplayObject ):DisplayObject
	{
		if ( this.asset != null && this.asset.parent != null )
		{
			this.asset.parent.removeChild( this.asset );
		}
		if ( Std.is( this.sigslotParent, Controller ) )
		{
			cast( cast( this.sigslotParent, Controller ).asset, DisplayObjectContainer ).addChild( d );
		}
		return this.asset = d;
	}
	
	override public function addChild(ichild:ISigSlot):Void 
	{
		var child = Reflect.field( ichild, "sigslot" );
		if ( Std.is( child, Controller ) )
		{
			cast( this.asset, DisplayObjectContainer ).addChild( cast( child, Controller ).asset );
		}
		super.addChild(ichild);
	}
	
	@slot override function onRemoveFromParent():Void 
	{
		super.onRemoveFromParent();
		if( this.asset != null && this.asset.parent != null ) this.asset.parent.removeChild( this.asset );
	}
}